#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 039

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from itertools import takewhile
from euler.numbers.geometry import pythagorean_triple
from euler.numbers.advanced_math import factors
from euler.list_functions import max_by

def get_answer():    
    """Question:
    
    If p is the perimeter of a right angle triangle with integral length sides, 
    {a,b,c}, there are exactly three solutions for p = 120.

    {20,48,52}, {24,45,51}, {30,40,50}
    
    For which value of p ≤ 1000, is the number of solutions maximised?
    """

    #The maximum value of p.
    target = 1000
    
    #The pythagorean triplets (a, b, c in a**2 + b**2 = c**2) which have a 
    #sum less than p. 
    pythagorean_triplets = list(
                                takewhile(
                                          lambda triplet: sum(triplet) < target,
                                          (pythagorean_triple(m, n)
                                                for m in xrange(1, target) 
                                                for n in xrange(m + 1, target))
                                        )
                                )
                                    
    def number_of_solutions(p):
        """Returns the number of solutions for a right angled triangle with
        perimeter [p].
        """        
        #All the factors of p.
        factors_of_p = set(factors(p))
        
        #Return all the triplets which have a sum equal to p or one of it's
        #factors. If a triplet evaluates to a factor of p, then one of its
        #multiples is equal to p. 
        return len(
                   list(
                        triplet for triplet 
                            in takewhile(
                                         lambda triplet: sum(triplet) <= p,
                                         pythagorean_triplets
                                        )
                            if sum(triplet) in factors_of_p
                        )
                )
    
    
    #Return result.
    return max_by(
                 lambda p : number_of_solutions(p),
                 (p for p in xrange(target + 1))
                )

    
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
